public without sharing class InstrumentScrappingHandler {
    	
    	//带入成本价
    	public static void bringCost(List<InstrumentScrapping__c> list_new ){

    		Set<Id>equips  = new Set<Id>();
    		Map<Id,User> uMap = new Map<Id,User>();

			for(InstrumentScrapping__c ins : list_new){
				if(ins.Status__c != '草案中' && ins.Status__c!= '已拒绝')continue;
				if(ins.ScrapInstrument__c != null )equips.add(ins.ScrapInstrument__c);
				uMap.put(ins.OwnerId, null);
			}
			if(equips.size() == 0)return;
			Map<Id,Id> peMap = new Map<Id,Id>();
			Set<Id> pids = new Set<Id>();

			for(Equipment__c e : [select Id,Product__c from Equipment__c where Id in: equips ]){


				pids.add(e.Product__c);

				peMap.put(e.Id, e.Product__c);


			}


			Set<String> companys = new Set<String>();
			Set<String> provinces = new Set<String>();
			for (User u :  [Select Id ,SubCompany__c,PriceProvince__c from User where Id in: uMap.keySet() ] ) {
				uMap.put(u.Id , u);
				companys.add(u.SubCompany__c);
				provinces.add(u.PriceProvince__c);

			}
			Map<String ,Price__c> prMap  = new Map<String,Price__c>();

			for(Price__c pr : [select Id ,Company__c,Province__c ,Product__c,InstCost__c from Price__c  where Product__c in: pids and Company__c in:companys and Province__c in:provinces  and ClosePrice__c != true  ]){

				prMap.put(pr.Product__c +'-'+ pr.Company__c+'-'+ pr.Province__c , pr );

			}


			for(InstrumentScrapping__c ins : list_new){
				    User u = uMap.get(ins.OwnerId);

					String key = peMap.get(ins.ScrapInstrument__c)+'-'+u.SubCompany__c+'-'+u.PriceProvince__c;
					Price__c p = prMap.get(key);

					if( p != null  )ins.SubcompanyCost__c = p.InstCost__c;
					
			}





    	}



    	//审批成功之后 标记仪器为报废 ,并锁定仪器
    	public static void backToEquipemt(List<InstrumentScrapping__c> list_new ,Map<Id,InstrumentScrapping__c>oldMap ){

    		List<Equipment__c> eqList = new List<Equipment__c>();
    		for ( InstrumentScrapping__c  instru :list_new ) {
    			InstrumentScrapping__c old = oldMap.get(instru.Id);
    			if(instru.Status__c == '已批准' && old.Status__c != '已批准'){

    				if(instru.ScrapInstrument__c != null ){
    					
				
						Equipment__c eqp=new Equipment__c(
										id=instru.ScrapInstrument__c
										,type__c='报废');
						
						eqList.add(eqp);
				
    				}
    			}
    		}
    		if(eqList.size() > 0){
    			update  eqList;
				Approval.lock(eqList,false);
    		}

    	}
}